/**
 *  @file RacerProResult.java
 *  @author Mamontov
 *
 *  This file contains RacerProResult class definition
 */
package db21.racerpro;

import com.racersystems.jracer.RacerList;
import com.racersystems.jracer.RacerSymbol;
import java.util.ArrayList;

/**
 * @class  RacerProResult
 * @author Mamontov
 *
 *
 */
public class RacerProResult {
    RacerList<RacerList<RacerList<RacerSymbol>>> m_result;
    String m_prefix;
    
    public RacerProResult(RacerList<RacerList<RacerList<RacerSymbol>>> result, 
                          String prefix) {
        m_result = result;
        m_prefix = prefix;
    } 
    
    
    
    
    public ArrayList<String> asRow() {
        ArrayList<String> result = new ArrayList<String>();
        for (RacerList<RacerList<RacerSymbol>> bindings : m_result) {
            for (RacerList<RacerSymbol> binding : bindings) {
               String q = binding.getValue().get(1).getValue();                        
               result.add(Queries.unescapeValue(m_prefix, q));
            }
        } 
        return result;
    }
    
    public ArrayList<String> header() {
        ArrayList<String> result = new ArrayList<String>();
        if (!m_result.getValue().isEmpty() ) {
           RacerList<RacerList<RacerSymbol>> firstRow = m_result.getValue().get(0);
           for (RacerList<RacerSymbol> binding : firstRow) {
               String q = binding.getValue().get(0).getValue();
               result.add(q);
            }
        }
        return result;
    }
    
    public ArrayList<String> row(int index) {
         ArrayList<String> result = new ArrayList<String>();
         for (RacerList<RacerList<RacerSymbol>> bindings : m_result) {
            String q = bindings.value.get(index).getValue().get(1).getValue();
            result.add(Queries.unescapeValue(m_prefix, q));
        } 
        return result;
    }
    
    public ArrayList<String> row(String pred) {
        ArrayList<String> header = this.header();
        int index = header.indexOf(pred);
        if (index < 0 ) {
            return new ArrayList<String>();
        }
        return this.row(index);
    }
}
